package com.kadron.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.kadron.base.KVBase;
import com.kadron.paging.KRow;

@Entity
@Table(name = "k_order_item")
public class OrderItem extends KVBase implements Serializable, KRow {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7074778235527470418L;

	private Order order;
	private Item item;

	private String itemColor;
	private String itemSize;
	private String itemMaterial;

	private int quantity;
	private double price;
	private double discountPrice;
	private double yourPrice;
	private long kadrons;
	
	private OrderItem() {
		super();
	}
	
	public OrderItem(Order order, Item item, String itemColor,
			String itemSize, String itemMaterial, int quantity, double price, double discountPrice, double yourPrice, long kadrons) {
		this();
		this.order = order;
		this.item = item;
		this.itemColor = itemColor;
		this.itemSize = itemSize;
		this.itemMaterial = itemMaterial;
		this.quantity = quantity;
		this.price = price;
		this.discountPrice = discountPrice;
		this.yourPrice = yourPrice;
		this.kadrons = kadrons;
	}

	@ManyToOne
	@JoinColumn(name = "order_id",nullable = false, updatable = false)
	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	@ManyToOne
	@JoinColumn(name = "item_id", nullable = false, updatable = false)
	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	@Column(name = "itemcolor", updatable = false)
	public String getItemColor() {
		return itemColor;
	}

	public void setItemColor(String itemColor) {
		this.itemColor = itemColor;
	}

	@Column(name = "itemsize", updatable = false)
	public String getItemSize() {
		return itemSize;
	}

	public void setItemSize(String itemSize) {
		this.itemSize = itemSize;
	}

	@Column(name = "itemmaterial", updatable = false)
	public String getItemMaterial() {
		return itemMaterial;
	}

	public void setItemMaterial(String itemMaterial) {
		this.itemMaterial = itemMaterial;
	}

	@Column(name = "quantity", nullable = false, updatable = false)
	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	@Column(name = "price", nullable = false, updatable = false)
	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Column(name = "discount_price", updatable = false)
	public double getDiscountPrice() {
		return discountPrice;
	}

	public void setDiscountPrice(double discountPrice) {
		this.discountPrice = discountPrice;
	}

	@Column(name = "your_price", updatable = false)
	public double getYourPrice() {
		return yourPrice;
	}

	public void setYourPrice(double yourPrice) {
		this.yourPrice = yourPrice;
	}

	@Column(name = "kadrons", nullable = false, updatable = false)
	public long getKadrons() {
		return kadrons;
	}
	
	public void setKadrons(long kadrons) {
		this.kadrons = kadrons;
	}
	
	@Transient
	public BigDecimal getSum() {
		BigDecimal quant = new BigDecimal(quantity);
		BigDecimal sum = null;
		BigDecimal dPrice = new BigDecimal(yourPrice);
		sum = dPrice.multiply(quant);
		sum = sum.setScale(2, RoundingMode.HALF_UP);
		return sum;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((item == null) ? 0 : item.hashCode());
		result = prime * result
				+ ((itemColor == null) ? 0 : itemColor.hashCode());
		result = prime * result
				+ ((itemMaterial == null) ? 0 : itemMaterial.hashCode());
		result = prime * result
				+ ((itemSize == null) ? 0 : itemSize.hashCode());
		result = prime * result + ((order == null) ? 0 : order.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OrderItem other = (OrderItem) obj;
		if (item == null) {
			if (other.item != null)
				return false;
		} else if (!item.equals(other.item))
			return false;
		if (itemColor == null) {
			if (other.itemColor != null)
				return false;
		} else if (!itemColor.equals(other.itemColor))
			return false;
		if (itemMaterial == null) {
			if (other.itemMaterial != null)
				return false;
		} else if (!itemMaterial.equals(other.itemMaterial))
			return false;
		if (itemSize == null) {
			if (other.itemSize != null)
				return false;
		} else if (!itemSize.equals(other.itemSize))
			return false;
		if (order == null) {
			if (other.order != null)
				return false;
		} else if (!order.equals(other.order))
			return false;
		return true;
	}


}
